<?php

class SchedulerController extends Controller
{
   
    public $menu = array(
        array(
            'label' =>  '调度器',
        ),
        array(
            'label' =>  '调度时钟',
            'icon'  =>  'time',
            'url'   =>  array('/scheduler/index'),
        ),
        array(
            'label' =>  '调度器状态',
            'icon'  =>  'adjust',
            'url'   =>  array('/scheduler/change'),
        ),
    );
    
    /**
     * @return array action filters
     * 
     */
    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function accessRules()
    {
        return array(
        
            array('allow', // allow authenticated users to access all actions
                'users'=>array('@'),
                'expression'=>'$user->role == Role::Admin',
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
                                                                                                                                            );
    }
	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		// renders the view file 'protected/views/schedule/index.php'
		// using the default layout 'protected/views/layouts/main.php'

        $criteria = new CDbCriteria(array(
            'order' =>  'date DESC'
        ));
        $dataProvider = new CActiveDataProvider('Schedule', array(
            'pagination'    =>  array(
                'pageSize'  =>  20,
            ),
            'criteria'  =>  $criteria,
        ));
        $this->render('index', array(
            'dataProvider'  => $dataProvider,
        ));
    }

    public function actionChange()
    {
        $model = ScheduleControl::model()->find();
        // collect user input data
        if(isset($_POST['ScheduleControl']))
        {
            $model->suspend=$_POST['ScheduleControl']['suspend'];
            if($model->save()) {
                Yii::app()->user->setFlash('success', '<strong>修改成功!</strong>调度器状态修改成功.');
            }
            else {
                Yii::app()->user->setFlash('error', '<strong>修改失败!</strong>调度器状态修改失败.');
            }
        }
        $this->render('change',array('model'=>$model));
    }
}
